﻿#include "wordcloundframe.h"
#include <qdebug.h>

WordCloundFrame::WordCloundFrame(QWidget *parent) : QWidget(parent)
{
    testData = QStringList{QStringLiteral("紅樓夢"),QStringLiteral("賈寶玉"),QStringLiteral("林黛玉"),QStringLiteral("薛寶釵")
            ,QStringLiteral("王熙鳳"),QStringLiteral("李紈"),QStringLiteral("賈元春"),
           QStringLiteral("賈迎春"),QStringLiteral("賈探春"),QStringLiteral("賈惜春"),
            QStringLiteral("秦可卿"),QStringLiteral("賈巧姐"),QStringLiteral("史湘雲"),QStringLiteral("妙玉"),
            QStringLiteral("賈政"),QStringLiteral("賈赦"),QStringLiteral("賈璉"),QStringLiteral("賈珍"),QStringLiteral("賈環"),
            QStringLiteral("賈母"),QStringLiteral("王夫人"),QStringLiteral("薛姨媽"),QStringLiteral("尤氏"),QStringLiteral("平兒"),
            QStringLiteral("鴛鴦"),QStringLiteral("襲人"),QStringLiteral("晴雯"),QStringLiteral("香菱"),QStringLiteral("紫鵑"),
            QStringLiteral("麝月"),QStringLiteral("小紅"),QStringLiteral("金釧"),QStringLiteral("甄士隱"),QStringLiteral("賈雨村")
};
}

void WordCloundFrame::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    QRect tagCanvas = this->rect(); //document.getElementById('html-canvas');
    QPoint center = WordCloundManager::getCenterPoint(tagCanvas);
    qDebug()<<Q_FUNC_INFO<<center;
    int cellSpace = 20;
    int maxRadius = WordCloundManager::getMaxRadius(tagCanvas, cellSpace);
    drawArray.clear();
    for(int i =0; i< testData.length(); i++){
        QString dataItem = testData[i];
        QJsonObject obj;
        QVector2D drawPosition;
        QJsonObject textInfo;
        if(i != 0){
            textInfo = WordCloundManager::getTextInfo(dataItem,obj);
            drawPosition = WordCloundManager::getDrawPosition(textInfo, maxRadius, center, cellSpace, drawArray);
        }else{
            obj.insert("fontsize",60);
            textInfo = WordCloundManager::getTextInfo(dataItem,obj);
            drawPosition = WordCloundManager::getDrawPosition(
                        textInfo,
                        maxRadius,
                        QPoint(center.x()-(textInfo.value("width").toInt()/2), center.y()+(textInfo.value("height").toInt()/2)),
                    cellSpace,
                    drawArray
                    );
            drawPosition.setY(drawPosition.y()+20);
        }
        if(!drawPosition.isNull()){
            WordCloundManager::drawText(drawPosition.toPoint(), textInfo, &p);
        }
    }
}
